/*
 *  $TEAM_NAME Space Trader
 */
package views;

import models.Player;
import models.Ship;
import presenters.ShipPresenter;

import javax.swing.JPanel;

import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.Dimension;

import java.util.ArrayList;
import java.util.List;

/**
 * Ship Table Panel
 * @author twb
 * @version 1.0
 */
public class ShipTablePanel extends JPanel {
	/**
	 * 40
	 */
	private static final int FORTY = 40;
	
	/**
	 * 2
	 */
	private static final int TWO = 2;
	
	/**
	 * field
	 */
	private final Player player;
    
	/**
	 * field
	 */
	private final List<ShipPanel> shipViews;
    
    /**
     * Constructs a table to hold the items being sold at the ship yard
     * 
     * @param player The game's player
     */
    public ShipTablePanel(Player player) {
        super.setLayout(null);
        super.setBorder(new LineBorder(Color.GRAY));
        super.setBackground(Color.BLACK);
        this.player = player;
        shipViews = new ArrayList<ShipPanel>();
        
        //Add Refueling 
        final ShipPanel fuel = new ShipPanel(player);
        ShipPresenter shipPresenter = new ShipPresenter(player, fuel);
        super.add(fuel);
        shipViews.add(fuel);
        
        //Add Repairing 
        final ShipPanel repair = new ShipPanel(player, player.getShip().getHealth());
        final ShipPresenter ship2Presenter = new ShipPresenter(player, repair);
        super.add(repair);
        shipViews.add(repair);

        //Add Ships to the ship yard table
        for (Ship ship: Ship.getShips()) {
    		ShipPanel view = new ShipPanel(player, ship);
    		shipPresenter = new ShipPresenter(player, view);
    		super.add(view);
    		shipViews.add(view);
        }
    }

    /*
     * Update all the rows based on the current system and the ships fuel level
     */
    /**
     * Method update.
     */
    public void update() {
    	for (ShipPanel view : shipViews) 
            view.update();
    }

    /**
     * Sets the bounds of all of the rows to be added
     */
    public void layoutSubviews() {
        final Dimension size = getSize();

        int y = 0;
        for (ShipPanel view : shipViews) {
            view.setBounds(0, y, size.width, FORTY);
            y += FORTY;
        }
        
    }

    /**
     * Sets the bounds of the ship yard table
     * 
     * @param x X-Position
     * @param y Y-Position
     * @param width Width of the table
     * @param height Height of the table
     */
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        layoutSubviews();
    }
    
    /**
     * Gets the number of rows based on planet tech level
     * 
     * @return ship views
     */    
    public int getNumShipViews(){
    	int views = 0;
    	for (Ship ship: Ship.getShips()){
    		int planetTech = player.getLocation().getTechLevel().ordinal();
    		if (ship.getMinTechLevel().ordinal() <= planetTech){
    			views++;
    		}
    	}
    	return views + TWO;
    }
}
